// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.itemSetting.propertySetting
{
    import com.bit101.components.ComboBox;

    import data.vo.LayoutItemVo;
    import data.vo.LayoutItemVoManger;
    import data.vo.LayoutItemVoPropertyPorxy;

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.text.TextField;

    import gui.itemSetting.propertySetting.relateScreenChoice.RelateScreenChoiceComponent;

    import utils.GUIUtils;
    import utils.MessageBox;

    public class ItemPropertySettingComponent
    {

        private var mRelativeValueTF:TextField;
        private var mRelativeTargetComboBox:ComboBox;
        private var mRelativeTypeComboBox:ComboBox;

        private var mPropertyType:int;
        private var mComponentMc:MovieClip;

        private var mRelativeScreenChoice:RelateScreenChoiceComponent;

        private var mItemVo:LayoutItemVoPropertyPorxy;

        public function ItemPropertySettingComponent()
        {
        }

        public function initialize(_componentMc:MovieClip, _propertyType:int):void
        {
            mComponentMc = _componentMc;
            mPropertyType = _propertyType;

            mRelativeTargetComboBox = new ComboBox(null, 0, 0, "关联对象");
            GUIUtils.replaceTargetWithPH(mRelativeTargetComboBox, mComponentMc["relativeTargetPH"]);
            refreshRelativeTargetData();

            mRelativeTypeComboBox = new ComboBox(null, 0, 0, "关联类型");
            GUIUtils.replaceTargetWithPH(mRelativeTypeComboBox, mComponentMc["relativeTypePH"]);
            refreshRelativeTypeData();

            mRelativeValueTF = mComponentMc["relativeValueTF"];
            mRelativeValueTF.visible = false;

            mRelativeScreenChoice = new RelateScreenChoiceComponent();
            mRelativeScreenChoice.initialize(mComponentMc["relativeScreenChoiceMc"]);

            addListener();
        }

        /**
         * 根据传入的Vo刷新Component的显示
         */
        public function show(_vo:LayoutItemVoPropertyPorxy):void
        {
            mItemVo = _vo;

            //刷新Target的数据源
            refreshRelativeTargetData();

            //刷新屏幕关联的选择框
            mRelativeScreenChoice.refresh(mItemVo);

            //根据数据 选择 关联目标
            if (mRelativeScreenChoice.isRelateTargetIsDotID(mItemVo.relativeTarget))
            {
                mRelativeTargetComboBox.selectedItem = GlobalConfig.RELATIVE_TARGET_SCREEN;
                mRelativeScreenChoice.enable = true;
            }
            else
            {
                mRelativeTargetComboBox.selectedItem = mItemVo.relativeTarget;
                mRelativeScreenChoice.enable = false;
            }

            //根据数据 选择 关联类型
            for each(var relativeTypeComboItem:Object in mRelativeTypeComboBox.items)
            {
                if (relativeTypeComboItem["value"] == mItemVo.relativeValueType)
                {
                    mRelativeTypeComboBox.selectedItem = relativeTypeComboItem;
                    break;
                }
            }

            //根据数据 设置 关联值
            mRelativeValueTF.text = "" + mItemVo.relativeValue;
        }

        public function tryToSaveItemVo():Boolean
        {
            //================
            //==设置关联对象
            //================
            if (mRelativeTargetComboBox.selectedItem != null)
            {
                if (mRelativeTargetComboBox.selectedItem == GlobalConfig.RELATIVE_TARGET_SCREEN)
                {
                    if (!mRelativeScreenChoice.tryToSaveLayoutItemVo())
                    {
                        //没有设置屏幕的关联类型
                        return false;
                    }
                }
                else
                {
                    mItemVo.relativeTarget = mRelativeTargetComboBox.selectedItem as String;
                }
            }
            else
            {
                MessageBox.instance.showMsg("没有设置 关联对象");
                return false;
            }

            //=============
            //==设置关联类型
            //=============
            if (mRelativeTypeComboBox.selectedItem != null)
            {
                if (mRelativeTargetComboBox.selectedItem == GlobalConfig.RELATIVE_TARGET_SCREEN &&
                        (!(mRelativeTypeComboBox.selectedItem["label"] == GlobalConfig.RELATIVE_TYPE_PIXEL ||
                                mRelativeTypeComboBox.selectedItem["label"] == GlobalConfig.RELATIVE_TYPE_PERCENT)))
                {
                    MessageBox.instance.showMsg("关联对象为屏幕 但类型不是像素或百分比");
                    return false;
                }
                else
                {
                    mItemVo.relativeValueType = mRelativeTypeComboBox.selectedItem["value"];
                }
            }
            else
            {
                MessageBox.instance.showMsg("没有设置 关联类型");
                return false;
            }

            //=============
            //==设置关联值
            //=============
            mItemVo.relativeValue = int(mRelativeValueTF.text);

            return true;
        }

        //==================================
        //== Private Function
        //==================================

        private function addListener():void
        {
            mRelativeTargetComboBox.addEventListener(Event.SELECT, onSelectOneRelativeTarget);
            mRelativeTypeComboBox.addEventListener(Event.SELECT, onSelectOneRelativeType);
        }

        /**
         * 设置 关联对象 的数据源
         */
        private function refreshRelativeTargetData():void
        {
            mRelativeTargetComboBox.removeAll();
            mRelativeTargetComboBox.addItem(GlobalConfig.RELATIVE_TARGET_SCREEN);
            for each(var vo:LayoutItemVo in LayoutItemVoManger.instance.allLayoutItemVoVector)
            {
                mRelativeTargetComboBox.addItem(vo.itemID);
            }
        }

        /**
         * 设置 关联类型 的数据源
         */
        private function refreshRelativeTypeData():void
        {
            mRelativeTypeComboBox.removeAll();
            mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_PIXEL, value: GlobalConfig.RELATIVE_TYPE_PIXEL_INT});
            mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_PERCENT, value: GlobalConfig.RELATIVE_TYPE_PERCENT_INT});
            if (mPropertyType == GlobalConfig.LAYOUT_ITEM_VO_PROPERTY_TYPE_Y)
            {
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_TOP, value: GlobalConfig.RELATIVE_TYPE_TOP_INT});
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_MIDDLE, value: GlobalConfig.RELATIVE_TYPE_MIDDLE_INT});
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_BOTTOM, value: GlobalConfig.RELATIVE_TYPE_BOTTOM_INT});
            } else
            {
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_LEFT, value: GlobalConfig.RELATIVE_TYPE_LEFT_INT});
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_MIDDLE, value: GlobalConfig.RELATIVE_TYPE_MIDDLE_INT});
                mRelativeTypeComboBox.addItem({label: GlobalConfig.RELATIVE_TYPE_RIGHT, value: GlobalConfig.RELATIVE_TYPE_RIGHT_INT});
            }
        }


        /**
         * 点击 关联类型 ComboBox
         */
        private function onSelectOneRelativeTarget(e:Event):void
        {
            mRelativeScreenChoice.enable = mRelativeTargetComboBox.selectedItem == GlobalConfig.RELATIVE_TARGET_SCREEN;

        }

        private function onSelectOneRelativeType(e:Event):void
        {
            mRelativeValueTF.visible = (mRelativeTypeComboBox.selectedItem["label"] == GlobalConfig.RELATIVE_TYPE_PIXEL ||
                    mRelativeTypeComboBox.selectedItem["label"] == GlobalConfig.RELATIVE_TYPE_PERCENT);
        }

    }
}
